// pages/enterprise/home/home.js
var app = getApp();
import {
    ajaxPost,
    showToast
} from '../../../utils/util.js';
import IMController from '../../../IMJS/im'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: 0,
        limit: 10,
        selectVal: app.globalData.meetingSelectVal,
        noGet: false,
        addressVal: ['全部', '全部', '全部'],
        region: [{
                fullName: "全部",
                code: 0
            },
            {
                fullName: "全部",
                code: 0
            }
        ],
        bannerSource: [],
        footerSource: [{
                title: '选选',
                url: '/pages/enterprise/list/list',
                img: '/images/icon_footer_icon1.png',
                imgSet: '/images/icon_footer_icon1_set.png',
                select: false
            },
            {
                title: '招聘会',
                url: '',
                img: '/images/icon_footer_icon2.png',
                imgSet: '/images/icon_footer_icon2_set.png',
                select: true
            },
            {
                title: '我的',
                url: '/pages/enterprise/mine/mine',
                img: '/images/icon_footer_icon3.png',
                imgSet: '/images/icon_footer_icon3_set.png',
                select: false
            },
        ],
        listSource: []
    },
    /**
     * 调用省市区三级联动
     */
    addressClick(e) {
        console.log();
        this.setData({
            [e.currentTarget.dataset.type]: true
        })
    },
    /**
     * 省市区三级联动 确认回调
     */
    addressSelect(e) {
        let selectVal = e.detail.selectVal
        console.log('addressSelect', selectVal);
        this.setData({
            region: selectVal
        })
        this.setData({
            page: 0,
            noGet: false,
            listSource: [],
            selectVal: e.currentTarget.dataset.type,
        })
        this.getList()

    },
    clickSelect(e) {
        if (e.currentTarget.dataset.type !== this.data.selectVal) {
            this.setData({
                page: 0,
                noGet: false,
                selectVal: e.currentTarget.dataset.type,
                listSource: []
            })
            this.getList()
        }
    },
    getList() {
        let _this = this;
        if (_this.data.noGet) {
            return;
        }
        let data = {
            token: wx.getStorageSync('token'),
            page: ++_this.data.page,
            limit: _this.data.limit,
            source: 'wx',
            type: _this.data.selectVal,
            is_follow: 0,
            province: this.data.region[0].fullName,
            city: this.data.region[1].fullName,

        };
        if (!this.data.selectVal) {
            data.type = 1
        }
        ajaxPost("/meeting/meetinglist", data, res => {
            if (res.code == 0) {
                let data = res.data;
                if (data.length == 0) {
                    _this.setData({
                        noGet: true
                    })
                } else {
                    let newarr = _this.data.listSource.concat(data)
                    _this.setData({
                        listSource: newarr,
                    })
                }
            } else {
                showToast('text', res.msg)
            }
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log('app.globalData.inviteVal', app.globalData.inviteVal);
        if (!!app.globalData.wytoken && !!app.globalData.account) {
            new IMController({
                token: app.globalData.wytoken,
                account: app.globalData.account,
            })
        }
        this.setData({
            page: 0,
            listSource: [],
            noGet: false,
        })
        this.getList()
        this.getBanner()

    },
    getBanner() {
        let data = {
            token: wx.getStorageSync('token')
        }
        ajaxPost('/index/banner', data, (res) => {
            if (res.code == 0) {
                this.setData({
                    bannerSource: res.data
                })
            }
        })
    },
    /**
     * 去详情页
     * @param {*} e  index
     */
    goCont(e) {
        let index = e.currentTarget.dataset.index
        let data = this.data.listSource[index]
        if (data.status == 1 && this.data.selectVal == 1 && data.app_status == 2) {
            wx.navigateTo({
                url: "/pages/enterprise/enterpriseCont/enterpriseCont?id=" + data.id + '&roomid=' + data.roomid
            });
        }
        if (data.status == 2 && this.data.selectVal == 1 && data.app_status == 2) {
            showToast('text', '招聘会暂未开场，请开场后进入')
            return;
        }
        if (data.app_status != 2) {
            wx.navigateTo({
                url: `/pages/web/web?url=${app.globalData.WEBURL}/h5/index/weibaoming`
            });
            return;
        }
        if (data.status !== 3 && this.data.selectVal == 2 && data.app_status == 2) {
            let studentInfo = this.data.listSource[index]
            wx.navigateTo({
                url: "/pages/enterprise/code/code?id=" + studentInfo.id
            });
        }
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getList()
    },
    /**
     * 地址更改
     */
    addressChange(e) {
        let val = e.detail.value,
            addressVal = this.data.addressVal
        if (val[0] == addressVal[0] && val[2] == addressVal[2] && val[3] == addressVal[3]) {
            return;
        }
        this.setData({
            addressVal: val
        })
        this.setData({
            page: 0,
            noGet: false,
            listSource: []
        })
        this.getList()
    }
})